#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

int keyboard()
{
	char buf[10] = {0};
	fgets(buf,sizeof(buf),stdin);
	buf[strlen(buf) - 1] = '\0';
	return atoi(buf);
}

int do_list()
{
	char buf[10] = {0};
	printf("1.播放目录\n");
	printf("2.暂停/继续\n");
	printf("3.停止\n");
	printf("4.上一个\n");
	printf("5.下一个\n");
	printf("6.快进\n");
	printf("7.定位\n");
	printf("8.播放方式\n");
	printf("9.退出\n");
	int ret = keyboard();
	return ret;
}

char list[100][20] = {0};
int i = 0;
void playname()
{
	i = 0;
	DIR *dir = opendir("./music/");
	if(NULL == dir)
	{
		perror("opendir");
		exit(1);
	}
	while(1)
	{
		struct dirent *info = readdir(dir);
		if(NULL == info)
		{
			break;
		}
		char *p[2] = {NULL};
		char name[512] = {0};
		strcpy(name,info->d_name);
		p[0] = strtok(name,".");
		if(NULL == p[0])
		{
			continue;
		}
		p[1] = strtok(NULL," ");
		if(0 == strcmp(p[1],"flv") || 0 == strcmp(p[1],"mp4") || 0 == strcmp(p[1],"mp3"));
		{
			strcpy(list[i],info->d_name);
			printf("%s\n",list[i]);
			++i;
		}
	}
	closedir(dir);
}

void play(int p)
{
	char name[20] = {0};
	sprintf(name,"./music/%s",list[p-1]);
	pid_t pid = fork();
	if(pid == 0)
	{
		execlp("mplayer","mplayer","-quiet","-slave","-input","file=/tmp/fifofile",name,NULL);
		exit(1);
	}
	else if(pid > 0)
	{
	}
	else
	{
		perror("fork");
		exit(1);
	}
}

void do_pause()
{
	int fd = open("/tmp/fifofile",O_WRONLY);
	if(-1 == fd)
	{
		perror("pause");
		exit(1);
	}
	write(fd,"pause\n",6);
	close(fd);
}

void do_stop()
{
	int fd = open("/tmp/fifofile",O_WRONLY);
	if(-1 == fd)
	{
		perror("stop");
		exit(1);
	}
	write(fd,"stop\n",5);
	close(fd);
}

int do_prev(int name)
{
	do_stop();
	if(name == 1)
	{
		name = i;
	}
	else
	{
		name--;
	}
	return name;
}

int do_next(int name)
{
	do_stop();
	if(name == i)
	{
		name = 1;
	}
	else
	{
		name++;
	}
	return name;
}

int j = 0;
void do_speed()
{
	int fd = open("/tmp/fifofile",O_WRONLY);
	if(-1 == fd)
	{
		perror("stop");
		exit(1);
	}
	else if(j == 0)
	{
		write(fd,"speed_set 0.75\n",15);
		++j;
	}
	else if(j == 1)
	{
		write(fd,"speed_set 1\n",12);
		++j;
	}
	else if(j == 2)
	{
		write(fd,"speed_set 1.25\n",15);
		++j;
	}
	else if(j == 3)
	{
		write(fd,"speed_set 1.5\n",14);
		j = 0;
	}
}

void do_seek(int seek)
{
	char buf[20] = {0};
	sprintf(buf,"seek %d 1\n",seek);
	int fd = open("/tmp/fifofile",O_WRONLY);
	if(-1 == fd)
	{
		perror("open");
		exit(1);
	}
	write(fd,buf,strlen(buf));
	close(fd);
}

int main(int argc, const char *argv[])
{
	printf("---------WELCOME TO USE---------\n");
	int ret = mkfifo("/tmp/fifofile",0666);
	if(-1 == ret && 0 == EEXIST)
	{
		perror("mkfifo");
		exit(1);
	}
	int name = 1;
	while(1)
	{
		int ret = do_list();
		if(ret == 1)
		{
			printf("quit:0\n");
			playname();
			name = keyboard();
			if(name == 0)
			{
				continue;
			}
			play(name);
		}
		else if(ret == 2)
		{
			do_pause();
		}							
		else if(ret == 3)
		{
			do_stop();
		}
		else if(ret == 4)
		{
			name = do_prev(name);
			play(name);
		}
		else if(ret == 5)
		{
			name = do_next(name);
			play(name);
		}
		else if(ret == 6)
		{
			do_speed();
		}
		else if(ret == 7)
		{
			int seek;
			printf("seek:");
			scanf("%d",&seek);
			do_seek(seek);
		}
		else if(ret == 8)
		{
			
		}
		else if(ret == 9)
		{
			printf("---------BYE---------\n");
			break;
		}
		else
		{
			printf("NOT FIND\n");
		}
	}
	remove("/tmp/fifofile");
	return 0;
}


